<script setup lang="ts">
import {ref} from "vue"
import {isEmpty} from "st-common-core"
import {useTipStore, TipMessageTypeConst, StnIpt} from 'st-ui-vue3-navie'
import {IptPropsPathEnum} from '@config'
import LoginForm from './LoginForm.vue'
import {loginByPwd} from "../apis/login-api.ts"

// 登录表单数据
const username = ref('root')
const password = ref('root')

/**
 * 校验登录表单数据
 *
 * @returns {boolean} true: 校验通过; false: 校验不通过
 */
const verify = (): boolean => {
  if (isEmpty(username.value)) {
    useTipStore().message('用户名不能为空', TipMessageTypeConst.ERROR)
    return false
  }
  if (isEmpty(password.value)) {
    useTipStore().message('密码不能为空', TipMessageTypeConst.ERROR)
    return false
  }
  return true
}

/**
 * 登录表单提交事件处理函数
 */
const submitHandler = async () => {
  // 校验登录表单数据
  if (!verify()) return
  // 密码登录
  return await loginByPwd(username.value, password.value)
}
</script>

<template>
  <div class="login-form-by-pwd">
    <LoginForm :submit-handler="submitHandler">
      <StnIpt
        v-model:value="username"
        :predefine-props="IptPropsPathEnum.USER_USERNAME"
      />
      <StnIpt
        v-model:value="password"
        :predefine-props="IptPropsPathEnum.USER_PASSWORD"
      />
    </LoginForm>
  </div>
</template>

<style scoped>

</style>
